package com.edu.ujn.myh.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts2.ServletActionContext;

import com.edu.ujn.myh.domain.ArticleInfo;
import com.edu.ujn.myh.domain.CategoryInfo;
import com.edu.ujn.myh.domain.CollegeInfo;
import com.edu.ujn.myh.domain.PrivInfo;
import com.edu.ujn.myh.domain.ProfessorInfo;
import com.edu.ujn.myh.domain.UserInfo;
import com.edu.ujn.myh.service.IApplyService;
import com.edu.ujn.myh.utils.DateUtils;
import com.edu.ujn.myh.utils.Encrypt;
import com.edu.ujn.myh.utils.FileOperation;
import com.edu.ujn.myh.utils.Pager;

/**
 * 
 * @作者 马永华
 * @日期 2010-4-14下午10:36:01
 * @版本 1.0.0
 */
public class ApplyAction extends BaseAction {

	private IApplyService applyService = null;

	private File applyFile;

	private String chinaName;

	private String englishName;

	private String chinaBrief;

	private String englishBrief;

	private String college;

	private Integer spState;

	private String filePath;

	private Integer fpState;

	private int categoryId;

	private String applyFileFileName;

	private String applyFileContentType;

	private String area = "";
	
	private int pageSize;

	private int currentPage;

	private int totalPage;
	
	private int type = 6;
	
	private int privId;
	
	private String privName;
	
	private String state;
	
	private String cateName;
	
	private int collegeId;
	
	private String collegeName;
	
	// 获得院系列表和文章种类列表
	public String getApplyInfo() {
		// 获得种类列表
		List categorylist = applyService.getCategory();
		// 获得院系列表
		List collegeList = applyService.getCollege();
		ServletActionContext.getRequest().setAttribute("categoryList",
				categorylist);
		ServletActionContext.getRequest().setAttribute("collegeList",
				collegeList);
		return SUCCESS;
	}

	// 将申请信息提交至数据库
	public String applyInfoPost() {

		String userName = (String) ServletActionContext.getRequest()
				.getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		ArticleInfo articleInfo = new ArticleInfo();
		articleInfo.setChinaBrief(chinaBrief.trim());
		articleInfo.setEnglishBrief(englishBrief.trim());
		articleInfo.setChinaName(chinaName.trim());
		articleInfo.setEnglishName(englishName.trim());
		articleInfo.setCollege(college.trim());
		articleInfo.setOpinions("");
		articleInfo.setSpState(0);
		articleInfo.setFpState(0);
		articleInfo.setScore(50);
		// 防止重复产生随机文件名
		String fileSaveName = FileOperation.getRandomFileName(applyFileFileName
				.trim());
		// 获得要存储文件的目录路径
		String path = FileOperation.getFileSavePath(ServletActionContext
				.getRequest());
		boolean flag = FileOperation.writeFile(applyFile, path + "/"
				+ fileSaveName);
		if (!flag) {
			return ERROR;
		}
		// 如果文件正常存储则将其他信息更新至数据库
		articleInfo.setFilePath(fileSaveName);
		articleInfo.setCreateDate(DateUtils.getDate(DateUtils.TYPE1));
		CategoryInfo categoryInfo = new CategoryInfo();
		categoryInfo.setId(categoryId);
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		articleInfo.setCategoryInfo(categoryInfo);
		articleInfo.setUserInfo(userInfo);
		String str = applyService.applyInfoSave(articleInfo);
		if ("error".equals(str)) {
			return ERROR;
		}
		return SUCCESS;
	}

	// 获得专家列表
	public String getProfessor() {
		List professorList = null;
		try {
			area = new String(area.getBytes("ISO8859_1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ("".equals(area)) {
			professorList = applyService.getProfessor("");
		} else {
			professorList = applyService.getProfessor(area);
		}
		getRequest().setAttribute("professorList", professorList);
		return SUCCESS;
	}
//	查看评阅历史记录
	public String getPinShenHistory(){
		String userName = (String) ServletActionContext.getRequest()
				.getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		
		Pager page = new Pager();
		page.setPageSize(10);
		page.setTotalPage(totalPage);
		page.setCurrentPage(currentPage);
		
//	 得到专家的Id
		int pId = (Integer)getRequest().getSession().getAttribute("pId");
		ProfessorInfo pi = new ProfessorInfo();
		pi.setId(pId);
		List list = applyService.getProfessArticle(pi,page);
		getRequest().setAttribute("articleList",list);
		getRequest().setAttribute("page", page);
		return SUCCESS;
		
	}
//	主持人查看审稿结果
//	0:等待审批，1:正在审批，2:未通过，3：已通过，4：未发表，5：已发表，6:all
	public String getZhuChiHistory(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		Pager page = new Pager();
		page.setPageSize(10);
		page.setTotalPage(totalPage);
		page.setCurrentPage(currentPage);
		List list = applyService.getArticleList(page,type,null);
		getRequest().setAttribute("articleList",list);
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
//	投稿者查看投稿稿件记录
	public String getUserArticleHistory(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		Pager page = new Pager();
		page.setPageSize(10);
		page.setTotalPage(totalPage);
		page.setCurrentPage(currentPage);
		List list = applyService.getArticleList(page,type,userInfo);
		getRequest().setAttribute("articleList",list);
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
//	获得正在进行评审的稿件
	public String getArticleState(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		Pager page = new Pager();
		page.setPageSize(10);
		page.setTotalPage(totalPage);
		page.setCurrentPage(currentPage);
		List list = applyService.getArticleList(page,6,userInfo);
		getRequest().setAttribute("articleList",list);
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
//	查看所有已通过的文章列表
	public String getArticleListPassed(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		Pager page = new Pager();
		page.setPageSize(10);
		page.setTotalPage(totalPage);
		page.setCurrentPage(currentPage);
		List list = applyService.getArticleList(page,3,null);
		getRequest().setAttribute("articleList",list);
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
//	*********************代码管理****************************
//	获得权限
	public String getPrivCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		List list  = applyService.getPrivList();
		getRequest().setAttribute("privList",list);
		return SUCCESS;
		
	}
//	更新权限信息
	public String modifyPrivCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			state  = "login";
			return SUCCESS;
		}
		PrivInfo p = new PrivInfo();
		p.setId(privId);
		p.setPrivName(privName);
		String str = applyService.modifyPriv(p);
		state  = str;
		return SUCCESS;
	}
//	获得稿件种类信息
	public String getCategoryCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		List list = applyService.getCategory();
		getRequest().setAttribute("categoryList",list);
		return SUCCESS;
	}
//	更改稿件种类名称
	public String modifyCategoryCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			state  = "login";
			return SUCCESS;
		}
		CategoryInfo cateInfo = new CategoryInfo();
		cateInfo.setId(categoryId);
		cateInfo.setName(cateName);
		state = applyService.modifyCategory(cateInfo);
		return SUCCESS;
	}
//	获得院系种类信息
	public String getCollegeCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		List list = applyService.getCollege();
		getRequest().setAttribute("collegeList",list);
		return SUCCESS;
	}
//	更改院系种类名称
	public String modifyCollegeCode(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			state  = "login";
			return SUCCESS;
		}
		CollegeInfo collegeInfo = new CollegeInfo();
		collegeInfo.setId(collegeId);
		collegeInfo.setName(collegeName);
		
		state = applyService.modifyCollege(collegeInfo);
		return SUCCESS;
	}
//	添加院系
	public String collegeAdd(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		CollegeInfo collegeInfo = new CollegeInfo();
		try {
			collegeInfo.setName(new String(collegeName.getBytes("ISO8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String str = applyService.collegeAdd(collegeInfo);
		return str;
	}
//	添加稿件种类
	public String categoryAdd(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
		CategoryInfo categoryInfo = new CategoryInfo();
		try {
			categoryInfo.setName(new String(cateName.getBytes("ISO8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String str = applyService.categoryAdd(categoryInfo);
		return str;
	}
//	上传excel文件将其中的数据导入数据库
	public String profeAdd(){
		String userName = (String) ServletActionContext.getRequest().getSession().getAttribute("userName");
		if (userName == null || "".equals(userName)) {
			return "login";
		}
//		获得Excel结构
		if(applyFile==null){
			return "error";
		}
		Workbook wb = null;
		try {
			wb = Workbook.getWorkbook(applyFile);
		} catch (BiffException e) {
			e.printStackTrace();
			return "error";
		} catch (IOException e) {
			e.printStackTrace();
			return "error";
		}
		Sheet[] sheet = wb.getSheets();
		List list = new ArrayList();
		for(int i=0;i<sheet.length;i++){
			int rows = sheet[i].getRows();
			for(int j=0;j<rows;j++){
				Cell cell[] = sheet[i].getRow(j);
				if(cell!=null&&cell.length>0){
					ProfessorInfo proInfo = new ProfessorInfo();
					proInfo.setUserName(cell[0].getContents().trim());
					proInfo.setPassWord(Encrypt.MD5(cell[1].getContents().trim()));
					proInfo.setArea(cell[3].getContents().trim());
					proInfo.setCollege(cell[2].getContents().trim());
					list.add(proInfo);
				}
			}
		}
		String str  = applyService.saveProf(list);
		return str;
	}
	
	
	
	public IApplyService getApplyService() {
		return applyService;
	}

	public void setApplyService(IApplyService applyService) {
		this.applyService = applyService;
	}

	public File getApplyFile() {
		return applyFile;
	}

	public void setApplyFile(File applyFile) {
		this.applyFile = applyFile;
	}

	public String getChinaName() {
		return chinaName;
	}

	public void setChinaName(String chinaName) {
		this.chinaName = chinaName;
	}

	public String getEnglishName() {
		return englishName;
	}

	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}

	public String getChinaBrief() {
		return chinaBrief;
	}

	public void setChinaBrief(String chinaBrief) {
		this.chinaBrief = chinaBrief;
	}

	public String getEnglishBrief() {
		return englishBrief;
	}

	public void setEnglishBrief(String englishBrief) {
		this.englishBrief = englishBrief;
	}

	public String getCollege() {
		return college;
	}

	public void setCollege(String college) {
		this.college = college;
	}

	public Integer getSpState() {
		return spState;
	}

	public void setSpState(Integer spState) {
		this.spState = spState;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public Integer getFpState() {
		return fpState;
	}

	public void setFpState(Integer fpState) {
		this.fpState = fpState;
	}

	public int getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}

	public String getApplyFileFileName() {
		return applyFileFileName;
	}

	public void setApplyFileFileName(String applyFileFileName) {
		this.applyFileFileName = applyFileFileName;
	}

	public String getApplyFileContentType() {
		return applyFileContentType;
	}

	public void setApplyFileContentType(String applyFileContentType) {
		this.applyFileContentType = applyFileContentType;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setPrivId(int privId) {
		this.privId = privId;
	}

	public void setPrivName(String privName) {
		this.privName = privName;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setCateName(String cateName) {
		this.cateName = cateName;
	}

	public void setCollegeId(int collegeId) {
		this.collegeId = collegeId;
	}

	public void setCollegeName(String collegeName) {
		this.collegeName = collegeName;
	}
	
}
